package com.j180.controller.supGoods;

import com.j180.bean.PageBean;
import com.j180.bean.ResultBean;
import com.j180.bean.SupGoods;
import com.j180.bean.SupGoodsCount;
import com.j180.service.supGoods.ISupGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/supgoods")
public class SupGoodsController {
    @Autowired
    private ISupGoodsService iSupGoodsService;

    /**
     *
     * @param id 供应商的id
     * @param curpage 开始页数
     * @param pageSize 每页几条
     * @return
     */
    @GetMapping("/supGoodsAll/{id}/{curpage}/{pageSize}")
    public ResultBean<PageBean<SupGoods>> supGoodsAll(@PathVariable String id, @PathVariable String curpage,
                                                      @PathVariable String pageSize){

        ResultBean<PageBean<SupGoods>> resultBean = new ResultBean<>();
        PageBean<SupGoods> pageBean = iSupGoodsService.showSupGoodsBySupplierId(Integer.parseInt(id), Integer.parseInt(curpage), Integer.parseInt(pageSize));

        if(pageBean.getDatas() != null){
            resultBean.setCode(1);
            resultBean.setT(pageBean);
            resultBean.setMsg("操作成功");
        }else {
            resultBean.setCode(0);
            resultBean.setT(null);
            resultBean.setMsg("操作失败");
        }
        return resultBean;
    }

    /**
     * 查询某个供应商的某个供应货品信息
     * @param supId 供应商的id
     * @param suppId 商品的id
     * @return
     */
    @GetMapping("/lsp/{suppId}/{supId}")
    public ResultBean<SupGoods> showSupgoodsInfoBySupplier(@PathVariable int suppId,@PathVariable int supId){

        ResultBean resultBean = new ResultBean();
        SupGoods supGoods = iSupGoodsService.showSupGoodsByIdAndSupplierId(suppId,supId);
        if(supGoods != null){
            resultBean.setCode(1);
            resultBean.setT(supGoods);
            resultBean.setMsg("操作成功");
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("操作失败");
        }

        return resultBean;
    }

    @PostMapping("/add")
    public ResultBean addSupgoods(@RequestBody SupGoods supGoods){

        ResultBean resultBean = new ResultBean();

        int result = iSupGoodsService.addSupGoodsInfo(supGoods);

        if(result > 0){
            resultBean.setCode(1);
            resultBean.setMsg("操作成功");
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("操作失败");
        }

        return resultBean;
    }
    @PutMapping("/update")
    public ResultBean updateSupgoods(@RequestBody SupGoods supGoods){

        ResultBean resultBean = new ResultBean();

        int result = iSupGoodsService.updateSupGoodsInfo(supGoods);

        if(result > 0){
            resultBean.setCode(1);
            resultBean.setMsg("操作成功");
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("操作失败");
        }

        return resultBean;
    }

    @DeleteMapping("/delete/{id}")
    public ResultBean deleteSupgoods(@PathVariable int id){

        ResultBean resultBean = new ResultBean();

        int result = iSupGoodsService.deleteSupGoodsByIdInfo(id);

        if(result > 0){
            resultBean.setCode(1);
            resultBean.setMsg("操作成功");
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("操作失败");
        }

        return resultBean;
    }

    @GetMapping("/showSupGoodsInfoBySupplierId/{id}")
    public ResultBean<List<SupGoodsCount>> showSupGoodsInfoBySupplierId(@PathVariable("id")int id){
        ResultBean <List<SupGoodsCount>> SupGoodsInfoBySupplierId = new ResultBean<>();

        List<SupGoodsCount> supGoods = iSupGoodsService.getSupGoodsInfoBySupplierId(id);

        if(supGoods.size()==0){
            SupGoodsInfoBySupplierId.setCode(0);
            SupGoodsInfoBySupplierId.setMsg("没有查询到符合条件的数据");
            SupGoodsInfoBySupplierId.setT(null);
        }else{
            SupGoodsInfoBySupplierId.setCode(1);
            SupGoodsInfoBySupplierId.setMsg("查询到"+supGoods.size()+"条,符合条件的数据");
            SupGoodsInfoBySupplierId.setT(supGoods);
        }

        return SupGoodsInfoBySupplierId;
    }

    @GetMapping("/allInfo/{curPage}/{pageSize}")
    public ResultBean<PageBean<SupGoods>> getAllSupGoods(@PathVariable("curPage") int curPage, @PathVariable("pageSize") int pageSize){
        ResultBean<PageBean<SupGoods>> resultBean = new ResultBean<>();
        PageBean<SupGoods> pageBean = iSupGoodsService.getAllSupGoodsInfo(curPage, pageSize);

        if(pageBean.getDatas().size() == 0){
            resultBean.setCode(0);
            resultBean.setMsg("没有搜索到相关内容");
            resultBean.setT(pageBean);
        }else{
            resultBean.setCode(1);
            resultBean.setMsg("找到" + pageBean.getDatas().size() + "条内容");
            resultBean.setT(pageBean);
        }
        return resultBean;
    }
}